//
//  HOMObjectWrapper.m
//  HigherOrderMessaging
//
//  Created by Ofri Wolfus on 18/10/05.
//  Copyright 2005 Ofri Wolfus. All rights reserved.
//

#import "HOMObjectWrapper.h"
#import "HOMUtilities.h"
#include <objc/objc-class.h>


@implementation HOMObjectWrapper

//Return a new instance
+ (id)wrapperWithTarget:(id <NSObject>)aTarget oneShot:(BOOL)value {
	return [[[self alloc] initWithTarget:aTarget oneShot:value] autorelease];
}

//Init
- (id)initWithTarget:(id <NSObject>)aTarget oneShot:(BOOL)value {
	target = [aTarget retain];
	oneShot = value;
	return self;
}

//Clean up
- (void) dealloc {
	[target release]; target = nil;
	oneShot = NO;
	
	[super dealloc];
}

//Get our target
- (id <NSObject>)target {
	return target;
}

//Indicate what we really are
- (BOOL)isObjectWrapper {
	return YES;
}

//Pass back to our targate
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
	return [target methodSignatureForSelector:aSelector];
}

//Forward any message back to our target
- (void)forwardInvocation:(NSInvocation *)anInvocation {
	[anInvocation setTarget:target];
	[anInvocation invoke];
	
	if (oneShot)
		[self release];
}

//This should really do what it was made to do...
- (BOOL)respondsToSelector:(SEL)aSelector {
	BOOL result = (class_getInstanceMethod(self->isa, aSelector) ? YES : NO);
	
	if (!result)
		result = [target respondsToSelector:aSelector];
	
	return result;
}

@end
